const {UserRegisterError} = require("../error/error");
const {createUser,getUserinfo} = require('../service/user.service')
const jwt = require('jsonwebtoken')

class UserController {
    async userRegister(ctx,next) {
        try {
            const {username,password} = ctx.request.body
            const res = await createUser(username,password)
            ctx.body={
                code:0,
                message:"注册成功",
                result:res
            }
        }catch (err) {
            console.log(err)
            ctx.app.emit('error',UserRegisterError,ctx)
        }
    }
    async userLogin(ctx,next) {
        try{
            const {username} = ctx.request.body;
            const {password,...res} = await getUserinfo({username})
            ctx.body = {
                code:0,
                message:"登录成功",
                result:{
                    token:jwt.sign(res,'fjhtglxt',{expiresIn:'1d'})
                }
            }
        }catch (err) {
            console.log(err)
        }
    }
    async getInfo(ctx,next) {
        try {
            const {id} = ctx.state.user;
            const res = await getUserinfo({id})
            ctx.body = {
                code:0,
                message:'查询成功',
                result:res
            }
        }catch (err) {
            console.log(err)
        }
    }
}
module.exports = new UserController()
